package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class IndexController {

    @Autowired
    private ProductFeignClient productFeignClient;
    //首页  https://www.gmall.com/     **.gmall.com
/*    @GetMapping("/")
    public String index(Model model){
        List<Map<String, Object>> resultMapList = getData();
        model.addAttribute("list",resultMapList);
        return "index/index";
    }*/
    @GetMapping("/")
    public String index(){
        return "index";
    }

    @Autowired
    private TemplateEngine templateEngine;

    //现在生成静态化页面
    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml() throws IOException {
        //1: 模板 + 数据 == 输出
        //模板 页面  （标签：Thymeleaf ）
        //数据 一二三级分类
        Context context = new Context();
        context.setVariable("list",getData());
        //输出  D:\temp
        Writer writer = new OutputStreamWriter(new FileOutputStream("D:\\temp\\index.html"),"utf-8");
        //Writer writer = new PrintWriter("D:\\temp\\index.html","utf-8");
        templateEngine.process("index/index",context,writer);
        System.out.println("页面已经生成了");
        return Result.ok();
    }

    //
    public List<Map<String,Object>> getData(){
        //分类视图集合
        List<BaseCategoryView> baseCategoryViewList =
                productFeignClient.getBaseCategoryViewList();

        //Map1:K : category1Id 1  V: List<BaseCategoryView> 60
        //Map2:K : category1Id 2   V: List<BaseCategoryView> 15
        //Map2:K : category1Id 3   V: List<BaseCategoryView> N
        //Map2:K : category1Id  V: List<BaseCategoryView>
        //Map2:K : category1Id  V: List<BaseCategoryView>
        //Map2:K : category1Id 17  V: List<BaseCategoryView>
        Map<Long, List<BaseCategoryView>> category1IdListMap = baseCategoryViewList.stream().
                collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        //最终结果对象
        List<Map<String,Object>> resultMapList = new ArrayList<>();

        int index  = 0;
        for (Map.Entry<Long, List<BaseCategoryView>> category1IdEntry : category1IdListMap.entrySet()) {
            Map<String,Object> category1IdMap = new HashMap<>();

            List<BaseCategoryView> category1Value = category1IdEntry.getValue();
            category1IdMap.put("index",++index);
            category1IdMap.put("categoryId",category1IdEntry.getKey());
            category1IdMap.put("categoryName", category1Value.get(0).getCategory1Name());
            //子分类集合  二级分类数据
            Map<Long, List<BaseCategoryView>> category2IdListMap = category1Value.stream().collect(Collectors.
                    groupingBy(BaseCategoryView::getCategory2Id));
            List<Map<String,Object>> category2IdMapList = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> category2IdEntry : category2IdListMap.entrySet()) {
                Map<String,Object> category2IdMap = new HashMap<>();

                List<BaseCategoryView> category2Value = category2IdEntry.getValue();
                category2IdMap.put("categoryId",category2IdEntry.getKey());
                category2IdMap.put("categoryName", category2Value.get(0).getCategory2Name());
                //子分类集合  三级   List<Map>
                List<Map> mapList = category2Value.stream().map(baseCategoryView -> {
                    Map map = new HashMap();
                    map.put("categoryId", baseCategoryView.getCategory3Id());
                    map.put("categoryName", baseCategoryView.getCategory3Name());
                    return map;
                }).collect(Collectors.toList());
                category2IdMap.put("categoryChild", mapList);

                category2IdMapList.add(category2IdMap);
            }
            category1IdMap.put("categoryChild", category2IdMapList);
            System.out.println("一级分类：次数：" + index);

            resultMapList.add(category1IdMap);
        }
        return resultMapList;
    }

    /*@GetMapping("/hello")
    public String hello(Model model, HttpSession session){

        model.addAttribute("world","hello world!!!");
        session.setAttribute("hai", "刘浩文科生");

        List<String> list = new ArrayList<>();
        list.add("上海");
        list.add("广州");
        list.add("北京");
        list.add("重庆");
        model.addAttribute("list", list);
        model.addAttribute("num", 10);

        return "hello";

    }*/

}
